package com.biz.primus.management.order.controller;

import com.biz.primus.account.utils.SSOUtils;
import com.biz.primus.base.vo.PageResult;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.order.feign.OmsFeignClient;
import com.biz.primus.model.oms.enums.OrderDeliveryType;
import com.biz.primus.model.oms.enums.OrderPaymentState;
import com.biz.primus.model.oms.enums.OrderState;
import com.biz.primus.model.oms.enums.returns.AuditState;
import com.biz.primus.model.oms.enums.returns.ReturnState;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import com.biz.primus.model.oms.vo.OrderQueryVo;
import com.biz.primus.model.oms.vo.OrderReturnQueryVo;
import com.biz.primus.model.oms.vo.returns.AuditRequestVo;
import com.biz.primus.model.oms.vo.returns.OmsReturnVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @note: 售后申请控制层
 * @author: zzy
 * @careat: 2017-11-22 10:34
 **/
@Controller
@RequestMapping("aftermarketApplications")
public class AftermarketApplicationsController {

    @Autowired
    private OmsFeignClient omsFeignClient;

    /**
     * 售后申请列表(首页,跳转)
     * @return
     */
    @GetMapping
    public ModelAndView index () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("aftermarketApplications/list");
        return modelAndView;
    }

    /**
     * 列表跳转详情
     * @return
     */
    @GetMapping("details")
    public ModelAndView details () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("aftermarketApplications/details");
        return modelAndView;
    }

    /**
     * 售后申请(处理页面获取数据,查询,刷新)
     * @return 前端使用datatable 必须使用aadata为key返回
     * @Exc     分页参数错误
     */
    @GetMapping("list")
    @ResponseBody
    public Map<String, Object> list (ReturnState state, String orderCode, int start, int length) {
        // 分页数据判断(datatable 分页数是0开始的)
        if (start < 0 && length < 1) {
            try {
                throw new Exception("分页参数错误");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OrderReturnQueryVo orderQueryVo = new OrderReturnQueryVo();

        List<ReturnState> orderQueryVos = new LinkedList<>();

        // 查询=>审核状态
        orderQueryVos.add(state);
        // 设置订单编码
        if (StringUtils.isBlank(orderCode)) {orderQueryVo.setOrderCode(orderCode);}

        // 添加分页数据
        orderQueryVo.setPage(start+1);
        orderQueryVo.setSize(length);

//        PageResult<OmsReturnVo> omsOrderVoSpringPageVO = omsFeignClient.managementReturnQuery(orderQueryVo);
        List<OmsOrderVo> omsOrderVoSpringPageVO  = getAll();
        // 返回值
        Map<String, Object> result = new HashMap<>();
//        //数据总条数
//        result.put("iTotalRecords", omsOrderVoSpringPageVO.getTotalPage());
//        //显示的条数
//        result.put("iTotalDisplayRecords", omsOrderVoSpringPageVO.getCurrentPage());
//        result.put("aaData", omsOrderVoSpringPageVO.getContent());

        result.put("iTotalRecords", omsOrderVoSpringPageVO.size());
        //显示的条数
        result.put("iTotalDisplayRecords", omsOrderVoSpringPageVO.size());
        result.put("aaData", omsOrderVoSpringPageVO);
        return result;
    }

    /**
     * 审核退单详情(售后审核详情)
     * @param returnCode    退单编码
     * @return
     */
    @GetMapping("auditReturnDetail")
    @ResponseBody
    public OmsReturnVo auditReturnDetail(String returnCode) throws Exception {
        if (StringUtils.isBlank(returnCode)) {
            throw new Exception("售后审核详情,缺少订单编码");
        }
        return omsFeignClient.auditReturnDetail(returnCode);
    }

    /**
     * 审核退单
     * @param auditState    审核状态
     * @param remark        审核备注
     * @param returnCode    退单编号
     */
    @PostMapping("auditReturn")
    public void auditReturn(AuditState auditState, String remark, String returnCode) throws Exception {
        //审核状态不能为空
        if (auditState == null) {
            throw new Exception("审核退单,缺少审核状态");
        }
        //退单编号不能为空
        if (StringUtils.isBlank(returnCode)) {
            throw new Exception("审核退单,缺少退单编号");
        }
        AuditRequestVo auditRequestVo = new AuditRequestVo();
        auditRequestVo.setAuditResult(auditState);
//        auditRequestVo.setReturnCode(returnCode);
        auditRequestVo.setReturnCode("1564");
        //审核备注允许为空
        if (StringUtils.isNotBlank(remark)) {
            auditRequestVo.setRemark(remark);
        }
        //操作人id
        auditRequestVo.setOperator(SSOUtils.getUserOfLogin().getId());
        omsFeignClient.auditReturn(auditRequestVo);
    }

    private List<OmsOrderVo> getAll() {
        List<OmsOrderVo>  result = new LinkedList<>();
        for (int j = 0; j < 6; j++) {
            OmsOrderVo orderItemVO = new OmsOrderVo();
            orderItemVO.setCode("订单编码" + j);
            orderItemVO.setOrderSource("订单来源" + j);
            orderItemVO.setSelfRemark("商家备注" + j);
            orderItemVO.setDeliveryType(OrderDeliveryType.self_now);
            orderItemVO.setPaymentState(OrderPaymentState.cod);
            orderItemVO.setAmount(j);
            orderItemVO.setDeliveryType(OrderDeliveryType.self_now);
            result.add(orderItemVO);
        }
        return result;
    }
}
